import Taro, { Component } from '@tarojs/taro'
import { View, Button, Form } from '@tarojs/components'

import allExtrance from './../../config/allExtrance'
import loginConfig from './../../config/login'
import phoneLogin from './../../api/phoneLogin'
import './btn.less'

class Btn extends Component {
    static externalClasses = ['my-class']
    constructor(props) {
        super(props)
    }

    componentWillReceiveProps(nextProps) {
        console.log(this.props, nextProps)
    }

    componentDidMount() {
    }

    componentWillUnmount() { }

    componentDidShow() { }

    componentDidHide() { }

    formIdSubmit(e) {
        // formId.formid(e.detail.formId)
        //     .then(res => { })
        //     .catch(err => { console.log(err) })

    }

    openSetting(e) {
        this.triggerEvent('openSetting', {
            authSetting: e.detail.authSetting
        })
    }

    loginFail(tip) {
        loginConfig('worker').catch(err => console.log(err))
        allExtrance.util.Taro.showToast(tip)
    }

    getUserInfo(e) {
        let detail = e.detail
        if (detail.userInfo && detail.encryptedData && detail.iv) {
            allExtrance.util.Taro.setStorageSync('userInfo', detail.userInfo)
            Taro.login({
                success: (res) => {
                    detail.code = res.code
                    // login.getUserInfo(detail)
                    //     .then(res => {
                    //         allExtrance.emitter.emit('getUserInfo', detail.userInfo)
                    //         Taro.navigateBack()
                    //     })
                    //     .catch(err => {
                    //         console.log(err)
                    //         this.loginFail('登录失败请重试')
                    //     })
                }
            })
        }
    }

    getPhoneNumber(e) {
        let detail = e.detail

        if (!detail.encryptedData || !detail.iv) {
            return
        }
        allExtrance.util.Taro.setStorageSync('phone', '17826807023')
        phoneLogin.phoneLogin({ 'phone': '17826807023' })
        // me.phone(detail)
        //     .then(res => {
        //         if (res.phone) {
        //             allExtrance.util.Taro.setStorageSync('wxPhone', res.phone)
        //             this.$scope.triggerEvent('getPhone', {
        //                 phone: res.phone
        //             })
        //         } else {
        //             this.loginFail('获取失败请重试')
        //         }
        //     })
        //     .catch(err => { console.log(err) })
    }

    render() {
        // const { type, formType, openType, hoverClass,title } = this.props;
        return (
            <View className='btnCom'>
                <Form onSubmit={this.formIdSubmit} report-submit='true'>
                    <Button style={`background:${Taro.getStorageSync('baseColor')}`} className={`hereBtn ${Taro.getEnv() === Taro.ENV_TYPE.WEB && 'h5Btn'} ${this.props.type} my-class`} lang='zh_CN' form-type='submit' open-type={this.props.openType} hover-class={this.props.hoverClass} onGetUserInfo={this.getUserInfo} onGetPhoneNumber={this.getPhoneNumber} onOpenSetting={this.openSetting}>
                        {this.props.title}
                    </Button>
                </Form>
            </View>
        )
    }
}

export default Btn
